import React from 'react';
import ReactDom from 'react-dom';
import $ from 'webpack-zepto';
import CartList from './view/cart-list.js';
import Config from '../common/config.js';
import Loader from '../common/loader.js';
import Util from '../common/util.js';

export default class Cart extends React.Component{
    render(){
        Util.storage().remove('toBuyGoods');
        return (
            <div className="cart">
                {this.props.data ? <CartList data={this.props.data} appId={this.props.appId}></CartList> : this.empty()}
            </div>
        );
    };

    empty(){
        return (
            <div className="empty">
                <img src="images/bird.png"/>
                <p><span>购物车为空！</span><span>快去购物吧O(∩_∩)O</span></p>
            </div>
        );
    };
};

var appId = Util.appId();
$.ajax({
    url : Config.path('duolu/cart',['?uToken=' + Util.storage().get(appId)]),
    type : 'get',
    dataType : 'json',
    beforeSend : function(){
        Loader.start();
    },
    success : function(resp){
        if(resp.status == 401){
            window.location.href = 'index.html?appid=' + appId;
        }else{
            ReactDom.render(
                <Cart data={resp.data} appId={appId}></Cart>,
                $('#cart')[0]
            );
        }
        Loader.end();
    }
});
